/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.zhima;

import org.zhiqim.account.ZacSessionUser;
import org.zhiqim.account.api.pay.AlipayServer;
import org.zhiqim.account.dbo.user.UserCertification;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.ValidateAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.orm.ZTable;

import com.alipay.api.response.ZhimaCustomerEpCertificationCertifyResponse;
import com.alipay.api.response.ZhimaCustomerEpCertificationInitializeResponse;

public class ZhimaEpCertifyAction extends ValidateAction
{

    @Override
    protected void validate(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("epCertName", "企业名称不能为空"));
        request.addValidate(new IsNotEmpty("certName", "法人姓名不能为空"));
        request.addValidate(new IsNotEmpty("certNo", "法人身份证不能为空"));
    }

    @Override
    protected void perform(HttpRequest request) throws Exception
    {
        String epCertName = request.getParameter("epCertName");
        String epCertNo = request.getParameter("epCertNo");
        String epCertType = request.getParameter("epCertType");
        String certName = request.getParameter("certName");
        String certNo = request.getParameter("certNo");
        
        if ("NATIONAL_LEGAL_MERGE".equals(epCertType) && !Validates.isByteLen(epCertNo, 18, 18))
        {
            request.returnCloseDialog("社会统一信用代码长度必须18位");
            request.returnHistory();
            return;
        }
        
        if ("NATIONAL_LEGAL".equals(epCertType) && !Validates.isByteLen(epCertNo, 15, 15))
        {
            request.returnCloseDialog("营业执照长度必须15位");
            request.returnHistory();
            return;
        }
       
        ZacSessionUser sessionUser = request.getSessionUser(ZacSessionUser.class);
        long userId = sessionUser.getUserId();
        String transactionId = "ZM" + Ids.longId();

        ZhimaCustomerEpCertificationInitializeResponse resp = Global.getService(AlipayServer.class).epCertificationInit(transactionId, certName, certNo, epCertType, epCertName, epCertNo);
        if (!resp.isSuccess())
        {
            request.setRedirect("/account/authenticate.htm");
            return;
        }
        
        String bizNo = resp.getBizNo();

        UserCertification cert = new UserCertification();
        cert.setUserId(userId);
        cert.setBizNo(bizNo);
        cert.setCertificationStatus(0);
        cert.setCertName(certName);
        cert.setCertNo(certNo);
        cert.setEpCertType(epCertType);
        cert.setEpCertName(epCertName);
        cert.setEpCertNo(epCertNo);
        cert.setTransactionId(transactionId);
        cert.setCertificationEndTime(DateTimes.getNextDateTimeStringBySecond(10 * 60));

        Global.get(ZTable.class).insert(cert);
        
        ZhimaCustomerEpCertificationCertifyResponse certifyResp = Global.getService(AlipayServer.class).epCertificationCertify(bizNo);
        if (!certifyResp.isSuccess())
        {
            request.setRedirect("/account/authenticate.htm");
            return;
        }

        String url = certifyResp.getBody();
        request.setRedirect(url);
    }

}
